package com.cmes.core.exception;

import com.cmes.core.DefaultResponseCode;
import com.cmes.core.ResponseCode;
import com.cmes.core.ResponseCodeAccessor;
import com.cmes.core.ResponseCodeData;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;

/**
 * 定义错误码的全局异常基类
 *
 * @author Bruce.Gong
 * @since 1.0.0-SNAPSHOT
 */
@Getter
@Setter
@NoArgsConstructor
public class ResponseCodeException extends ApplicationException implements ResponseCodeAccessor {

    private ResponseCode responseCode;

    public ResponseCodeException(String message) {
        super(message);
    }

    public ResponseCodeException(String code, String message) {
        this(new DefaultResponseCode(code, message));
    }

    public ResponseCodeException(ResponseCode responseCode) {
        super(responseCode.getMessage());
        this.responseCode = responseCode;
    }

    public ResponseCodeException(ResponseCode responseCode, Object... args) {
        super(String.format(responseCode.getMessage(), args));
        this.responseCode = responseCode;
    }

    public <T> ResponseCodeData<T> response() {
        return new ResponseCodeData<T>().fail((ResponseCodeAccessor) this);
    }

}
